/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import logica.Cliente;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author AlisonPC
 */
public class ClienteDAO {

    private Session sessao;
    private Transaction transacao;

    public void salvar(Cliente cliente) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.save(cliente);
        transacao.commit();
        sessao.close();
    }

    public void remover(Cliente cliente) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.delete(cliente);
        transacao.commit();
        sessao.close();
    }
     public Cliente pesquisaCodigo(int codigo){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        Cliente dentista = (Cliente) sessao.createCriteria(Cliente.class).
                add(Restrictions.eq("codigo", codigo)).uniqueResult();
        sessao.close();
        return dentista;        
    }
    
    public List<Cliente> listar(){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        Criteria criteria = sessao.createCriteria(Cliente.class);
        List<Cliente> dentistas = criteria.list();
        sessao.close();
        return dentistas;
    }
    
    public List<Cliente> pesquisaNome(String nome){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        List<Cliente> dentistas = sessao.createCriteria(Cliente.class).
                add(Restrictions.like("nome",nome,MatchMode.ANYWHERE)).list();
        sessao.close();
        return dentistas;        
    }
    
    public List<Cliente> pesquisaCROLike(String CRO){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        Criteria criteria = sessao.createCriteria(Cliente.class);
        criteria.add(Restrictions.like("CRO", CRO, MatchMode.ANYWHERE));
        criteria.addOrder(Order.asc("CRO"));
        List<Cliente> clientes = criteria.list();
        return clientes;        
    }
    
    public List<Cliente> pesquisaUsuario(String usuario){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        List<Cliente> usuarios = sessao.createCriteria(Cliente.class).
                add(Restrictions.like("usuario",usuario,MatchMode.ANYWHERE)).list();
        sessao.close();
        return usuarios;        
    }
    
    public List<Cliente> pesquisaSenha(String senha){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        List<Cliente> senhas = sessao.createCriteria(Cliente.class).
                add(Restrictions.like("senha",senha,MatchMode.ANYWHERE)).list();
        sessao.close();
        return senhas;        
    }
    
}
